package com.jqr.activity.onclicklistenerdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    public Button btnOne,btnSecond1,btnSecond2,btnSecond3,btnThree1,btnThree2,btnThree3,btnClick;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnOne=findViewById(R.id.btnOne);
        btnSecond1=findViewById(R.id.btnSecond1);
        btnSecond2=findViewById(R.id.btnSecond2);
        btnSecond3=findViewById(R.id.btnSecond3);
        btnThree1=findViewById(R.id.btnThree1);
        btnThree2=findViewById(R.id.btnThree2);
        btnThree3=findViewById(R.id.btnThree3);

        //第一种：匿名函数

//        btnOne.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Toast.makeText(MainActivity.this,"匿名函数",Toast.LENGTH_SHORT).show();
//            }
//        });
//        btnSecond1.setOnClickListener(this);
//        btnSecond2.setOnClickListener(this);
//        btnSecond3.setOnClickListener(this);


        //第三种：内部类

        btnThree1.setOnClickListener(new MyClickListener());
        btnThree2.setOnClickListener(new MyClickListener());
        btnThree3.setOnClickListener(new MyClickListener());
        //第四种，不推荐
        btnClick=findViewById(R.id.btnClick);

    }
    //第二钟：继承接口 implements View.OnClickListener
//    @Override
//    public void onClick(View v) {
//       if(v.getId()==R.id.btnSecond1){
//           Toast.makeText(MainActivity.this,"继承接口1",Toast.LENGTH_SHORT).show();
//       }
//       else if(v.getId()==R.id.btnSecond2){
//           Toast.makeText(MainActivity.this,"继承接口2",Toast.LENGTH_SHORT).show();
//       }
//       else {
//           Toast.makeText(MainActivity.this,"继承接口2",Toast.LENGTH_SHORT).show();
//       }
//
//    }
    public void click(View View){
        Toast.makeText(MainActivity.this,"Click",Toast.LENGTH_SHORT).show();
    }

    //第三种：内部类
    private class MyClickListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            if(v.getId()==R.id.btnSecond1){
                Toast.makeText(MainActivity.this,"继承接口1",Toast.LENGTH_SHORT).show();
               }
               else if(v.getId()==R.id.btnSecond2){
                   Toast.makeText(MainActivity.this,"继承接口2",Toast.LENGTH_SHORT).show();
               }
               else {
                   Toast.makeText(MainActivity.this,"继承接口2",Toast.LENGTH_SHORT).show();
               }
        }
    }

}